package com.community.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.community.pojo.dto.ParkingQueryDto;
import com.community.pojo.entity.Parking;
import com.community.pojo.entity.Result;
import com.community.pojo.vo.ParkingListVo;
import com.community.service.ParkingService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.ai.tool.annotation.Tool;
import org.springframework.ai.tool.annotation.ToolParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Slf4j
@RequestMapping("/parking")
@Tag(name = "车位相关接口",description = "车位相关api")
public class ParkController {

    @Autowired //注入service
    private ParkingService parkingService;

    @GetMapping("/list")//配置请求路径
    @Operation(summary = "停车列表")
    @Tool(description = "查询停车列表")
    public Result list(@ToolParam ParkingQueryDto parkingQueryDto) throws Exception {

        //1.设置分页条件
        PageHelper.startPage(parkingQueryDto.getPage(),parkingQueryDto.getLimit());

        //2.查询
        List<ParkingListVo> parkingList = parkingService.findParkingListByQueryDto(parkingQueryDto);

        //3.获取分页结果
        PageInfo<ParkingListVo> pageInfo=new PageInfo<>(parkingList);

        //封装返回结果
        return Result.buildSuccess(pageInfo.getList(),pageInfo.getTotal());
    }



    @GetMapping("/findAll")
    @Operation(summary = "查询所有停车")
    @Tool(description = "查询所有停车")
    public Result findAll() throws Exception {

        //1.查询所有停车
        List<Parking> parkingList = parkingService.list();

        //2.封装返回结果
        return Result.buildSuccess(parkingList);
    }

    @GetMapping("/find")
    @Operation(summary = "根据id查询停车")
    @Tool(description = "根据id查询停车")
    public Result find(@ToolParam Long id){

        if(id==null){
            return Result.buildSuccess(null);
        }

        //1.根据id查询停车
        Parking parking = parkingService.getById(id);

        //2.封装返回结果
        return Result.buildSuccess(parking);
    }


    @PostMapping("/add")
    @Operation(summary = "添加停车")
    @Tool(description = "添加停车")
    public Result add(@ToolParam @RequestBody Parking parking){

        //1.保存数据
        parkingService.save(parking);

        //2.返回结果
        return Result.buildSuccess(null);
    }

    @PostMapping("/update")
    @Operation(summary = "修改停车")
    @Tool(description = "修改停车")
    public Result update(@ToolParam @RequestBody Parking parking){

        //1.保存数据
        parkingService.updateById(parking);

        //2.返回结果
        return Result.buildSuccess(null);
    }


    @GetMapping("/delete")
    @Operation(summary = "根据id删除停车")
    @Tool(description = "根据id删除停车")
    public Result delete(@ToolParam Long id) {
        if (id == null) {
            return Result.buildSuccess(null);
        }
        //1.根据id查询停车
        parkingService.removeById(id);

        //2.封装返回结果
        return Result.buildSuccess(null);
    }
}
